﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using Cinchoo.Core.Services;
using System.Collections;
using System.IO;
using Cinchoo.Core;
using Cinchoo.Core.Xml.Serialization;
using System.Runtime.Serialization;

namespace Cinchoo.Core.Configuration
{
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
    public sealed class ChoJSONConfigurationSectionAttribute : ChoConfigurationSectionAttribute
    {
        #region Instance Data Members (Private)

        private Type _configSectionHandlerType = typeof(ChoDictionarySectionHandler);
        private Type _configStorageType = typeof(ChoFileJSONSerializerConfigStorage);
        private Type _configStoragePayloadSerializer = typeof(ChoConfigStorageJSONPayloadSerializer);

        #endregion Instance Data Members (Private)

        #region Instance Properties (Public)

        [XmlIgnore]
        public override Type ConfigSectionHandlerType
        {
            get { return _configSectionHandlerType; }
            protected set { _configSectionHandlerType = value; }
        }

        public override Type ConfigStorageType
        {
            get { return _configStorageType; }
            set { throw new NotSupportedException(); }
        }

        public Type ConfigStoragePayloadSerializer
        {
            get { return _configStoragePayloadSerializer; }
            set
            {
                if (value == null) return;
                if (!(typeof(IChoConfigStoragePayloadSerializer).IsAssignableFrom(value))) return;

                _configStoragePayloadSerializer = value;
            }
        }

        #endregion Instance Properties (Public)

        #region Constructor

        public ChoJSONConfigurationSectionAttribute(string configElementPath)
            : base(configElementPath)
        {
        }

        #endregion Constructor

        #region ChoConfigurationSectionAttribute Overrides

        protected override ChoStandardConfigurationMetaDataInfo CreateMetaDataInfo(ChoBaseConfigurationElement configElement, Type type)
        {
            configElement.StateInfo["ConfigStoragePayloadSerializer"] = Activator.CreateInstance(ConfigStoragePayloadSerializer);
            return base.CreateMetaDataInfo(configElement, type);
        }

        #endregion ChoConfigurationSectionAttribute Overrides
    }
}